package openrr;

//imports
import org.lwjgl.LWJGLException;
import org.lwjgl.input.*;
import org.lwjgl.util.Timer;

//the Input class deals with handling user input
public class Input {
	//a reference to the main game class
	private GameMain game;
	//the next 3 variables are for limiting the movement speed of the player. Since a check is done whether keys are pressed on every frame, a small time interval is needed so that the player can let the player on the board stop moving when required.
	//after a request has been made to move the player, the initial time is read from the main game timer (the lwjgl Timer class), and stored here, so that it is possible to check how much time has elapsed since the player was last moved.
	private float startTime = 0;
	//a boolean to determine whether the program is waiting for the next time the player can be moved (i.e. until the delay period after each move has passed)
	private boolean isWaiting = false;
	//a reference to the main game timer
	private Timer timer;
	
	//constructor; creates the keyboard and mouse so that they can be worked with
	public Input(GameMain gameMain)
	{
		game = gameMain;
		try
		{
			Mouse.create();
			Keyboard.create();
		} catch(LWJGLException e) 
		{
			System.out.println("Error while creating the mouse: ");
			e.printStackTrace();
		}
		timer = new Timer();
	}
	
	//handles the mouse movement
	public void handleMouse()
	{
		//a check if the right mouse button is down
		if(Mouse.isButtonDown(1))
		{
			//moving the mouse left and right rotates the map
			game.mapRotation += (Mouse.getDX())/game.rotationFactor;
			//moving the mouse up and down tilts the map
			game.mapPitch -= (Mouse.getDY());
			
			//2 checks to make sure the player does not turn the world upside down
			if(game.mapPitch > 0)
			{
				game.mapPitch = 0;
			}	
			if(game.mapPitch < -400)
			{
				game.mapPitch = -400;
			}
			
		}
		//if the mouse has a mouse wheel, enable zooing using the mouse wheel
		if(Mouse.hasWheel())
		{
			int dw = Mouse.getDWheel();
			if(((game.mapZoomLevel + dw) > 0) && ((game.mapZoomLevel + dw) < 800))
			{
				game.mapZoomLevel += dw;
			}
			
		}
	}
	
	public void handleKeyboard()
	{
		//move the map in the direction the camera is looking (requires some geometrical calculations). The map's move speed is also affected by the zoom level. This means that it will move faster when zoomed out more
		if(Keyboard.isKeyDown(Keyboard.KEY_LEFT))
		{
			game.mapX += 0.02*Math.sin((game.mapRotation + 90)*Math.PI/180)*game.mapZoomLevel;
			game.mapY += 0.02*Math.cos((game.mapRotation + 90)*Math.PI/180)*game.mapZoomLevel;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
		{
			game.mapX -= 0.02*Math.sin((game.mapRotation + 90)*Math.PI/180)*game.mapZoomLevel;
			game.mapY -= 0.02*Math.cos((game.mapRotation + 90)*Math.PI/180)*game.mapZoomLevel;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_UP))
		{
			game.mapX -= 0.02*Math.sin(game.mapRotation*Math.PI/180)*game.mapZoomLevel;
			game.mapY -= 0.02*Math.cos(game.mapRotation*Math.PI/180)*game.mapZoomLevel;
		}
		if(Keyboard.isKeyDown(Keyboard.KEY_DOWN))
		{
			game.mapX += 0.02*Math.sin(game.mapRotation*Math.PI/180)*game.mapZoomLevel;
			game.mapY += 0.02*Math.cos(game.mapRotation*Math.PI/180)*game.mapZoomLevel;
		}
	}
}
